package com.yx.pc.api.quartz;

import com.yx.modules.api.service.SysAuthBookService;
import com.yx.system.entity.*;
import com.yx.system.service.*;
import com.yx.utils.HttpUtil;
import com.yx.utils.UidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 定时器
 *
 * @author luo
 * @date 2018/4/18
 */
@Component
@Configurable
@EnableScheduling
public class QuartzTask {

    @Autowired
    private SysAuthBookService sysAuthBookService;

    @Autowired
    private SysAuthBookInfoService sysAuthBookInfoService;

    @Autowired
    private TdSectionService tdSectionService;

    @Autowired
    private TdBookService tdBookService;

    @Autowired
    private TdTextService tdTextService;

    @Autowired
    private SysHanwuBookService sysHanwuBookService;

    @Autowired
    private SysHanwuBookInfoService sysHanwuBookInfoService;


    /*
    * 计算图书的章节数
    */
    @Scheduled(cron ="0 0 4 * * ?")//凌晨4点执行
    public void getSectionNumber(){
        //找到所有图书
        List<TdBook> tdBooks = tdBookService.queryAll();
        for (TdBook tdBook:tdBooks){
            List<TdSection> tdSections = tdSectionService.findByBookid(tdBook.getId());
            tdBook.setSectionNumber(tdSections.size());
            tdBookService.updateByPrimaryKey(tdBook);
        }
    }

    /*
    * 实时获取第三方图书存入数据库
    */
    @Scheduled(cron = "0 0 0/1 * * ?")//1小时执行一次
    public void paymentOrder(){
        String client_id = "49";
        String client_secret = "MTUzMTE5MDA5MjM5MQ";
        String sign = HttpUtil.encryption("49MTUzMTE5MDA5MjM5MQ");
        String temp = HttpUtil.sendGet("http://www.quyuewang.cn/api.php/BookList/index?client_id=49&sign="+sign);
        //通过第三方接口获取图书列表
        List<SysAuthBook> sysAuthBooks = HttpUtil.jsonToList(temp,SysAuthBook.class);
        //初始化一个真真需要存储的图书集合
        List<SysAuthBook> authBooks = new ArrayList<>(sysAuthBooks);
        if (sysAuthBooks.size()!=0){
            //获取数据库里面图书列表
            List<SysAuthBook> sysAuthBookList = sysAuthBookService.findAll();

            //数据库数据不为空就进行筛选
            if (sysAuthBookList!=null){
                //循环判断数据库存在就把这存在的数据移除集合
                for (int i = 0;i<sysAuthBooks.size();i++){
                    for (int j = 0;j<sysAuthBookList.size();j++){
                        if (sysAuthBookList.get(j).getId().equals(sysAuthBooks.get(i).getId())){
                            authBooks.remove(sysAuthBooks.get(i));
                            break;
                        }

                    }
                }
            }

            //判断进行保存
            if (authBooks.size()!=0){
                for (SysAuthBook sysAuthBook:authBooks){
                    sysAuthBookService.save(sysAuthBook);
                    String id = sysAuthBook.getId();
                    String sign2 = HttpUtil.encryption("49MTUzMTE5MDA5MjM5MQ"+id);
                    String temp2 = HttpUtil.sendGet("http://www.quyuewang.cn/api.php/BookInfo/index/"+id+"?client_id=49&sign="+sign2);
                    SysAuthBookInfo sysAuthBookInfo = HttpUtil.tobean(temp2,SysAuthBookInfo.class);
                    sysAuthBookInfo.setStatus(0);
                    sysAuthBookInfo.setCreateDate(new Date());
                    sysAuthBookInfo.setUuid("0");
                    sysAuthBookInfoService.save(sysAuthBookInfo);
                }
            }

        }



        String bookSign = HttpUtil.encryption("page=1&sid=20&key=0cPfpLrPEnt7MiCiNAh48asyjbDP2v8w");
        String bookTemp = HttpUtil.sendGet("http://www.hanwujinian.com/apis/sid/articlelist.php?page=1&sid=20&sign="+bookSign);
        //获取第三方的图书
        List<SysHanwuBook> sysHanwuBooks = HttpUtil.jsonToList(bookTemp,SysHanwuBook.class);
        //初始化一个真真需要存储的图书集合
        List<SysHanwuBook> RealList = new ArrayList<>(sysHanwuBooks);
        if (sysHanwuBooks.size()!=0){
            //从数据库取得存储的第三方图书
            List<SysHanwuBook> hanwuBooks = sysHanwuBookService.findAll();
            //把第三方获取的和数据库做对比，有相同的就把相同的移除
            if (hanwuBooks!=null){
                //循环判断数据库存在就把这存在的数据移除集合
                for (int i = 0;i<sysHanwuBooks.size();i++){
                    for (int j = 0;j<hanwuBooks.size();j++){
                        if (hanwuBooks.get(j).getArticleid().equals(sysHanwuBooks.get(i).getArticleid())){
                            RealList.remove(sysHanwuBooks.get(i));
                            break;
                        }
                    }
                }
            }
            //判断进行保存
            if (RealList.size()!=0){
                for (SysHanwuBook sysHanwuBook:RealList){
                    sysHanwuBookService.save(sysHanwuBook);
                    String id = sysHanwuBook.getArticleid();
                    String sign2 = HttpUtil.encryption("aid="+id+"&sid=20&key=0cPfpLrPEnt7MiCiNAh48asyjbDP2v8w");
                    String temp2 = HttpUtil.sendGet("http://www.hanwujinian.com/apis/sid/articleinfo.php?aid="+id+"&sid=20&sign="+sign2);
                    SysHanwuBookInfo sysHanwuBookInfo = HttpUtil.tobean(temp2,SysHanwuBookInfo.class);
                    sysHanwuBookInfo.setStatus(0);
                    sysHanwuBookInfo.setCreateDate(new Date());
                    sysHanwuBookInfo.setUuid("0");
                    sysHanwuBookInfoService.save(sysHanwuBookInfo);
                }
            }


        }
    }

    /*
    * 章节的定时发布
    */
    @Scheduled(cron = "0 0/5 * * * ?")//5分钟执行一次
    public void sectionPublish(){
        //取得当前时间
        Date date = new Date();
        //首先查找所有定时发布的章节
        List<TdSection> tdSections = tdSectionService.findPublishSection();
        //循环所有章节
        for (TdSection tdSection:tdSections){
            if (tdSection.getPublishDate().getTime()<=date.getTime()) {
                //找到章节对应的图书
                TdBook tdBook = tdBookService.selectByPrimaryKey(tdSection.getBookid());
                if (tdBook != null) {
                    tdBook.setNewsection(tdSection.getSectionname());
                    tdBook.setNewsectionid(tdSection.getId());
                    tdBook.setSectionNumber(tdBook.getSectionNumber() + 1);
                    tdBook.setUpdatedate(new Date());
                    tdBookService.updateByPrimaryKey(tdBook);
                    //创建Text对象
                    TdText tdText = new TdText();
                    String text = tdSection.getContext() + tdSection.getLead();
                    tdText.setText(text);
                    tdText.setIsdelete(0);
                    tdText.setCreatetime(new Date());
                    tdText.setAuthor(tdBook.getAuthorName());
                    tdText.setSection(tdSection.getSectionname());
                    tdText.setBookname(tdBook.getBookname());
                    tdText.setUid(tdSection.getUuid());
                    tdText.setId(UidUtil.getUUID());
                    tdTextService.saveText(tdText);
                    //改变章节状态
                    tdSection.setSectionStatus(0);
                    tdSectionService.updateByPrimaryKey(tdSection);
                }
            }
        }
    }
}
